package com.oscar.ehome.view.pageview;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.AppListBean;
import com.oscar.ehome.bean.pagebean.StudyBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshBase;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshListView;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.messageview.MessageDetilActivity;
import com.oscar.ehome.view.pageview.adapter.AppListAdapter;
import com.oscar.ehome.view.pageview.view.AppListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/7/23
 * 应用列表
 */

public class AppListActivity extends BasesActivity<AppListView, PagePresenter> implements AdapterView.OnItemClickListener, AppListView, PullToRefreshBase.OnRefreshListener {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     * 返回
     */
    @BindView(R.id.app_listview)
    PullToRefreshListView appListview;
    /**
     * title布局
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;

    List<AppListBean.AppList> list = new ArrayList<>();
    AppListAdapter adapter;
    PagePresenter pagePresenter;

    String token = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_applist);
        ButterKnife.bind(this);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(AppListActivity.this).getSystemConfig().getColor()));
        onClick();
        initView();
        initData();
    }

    @Override
    protected PagePresenter createPresenter() {
        pagePresenter = new PagePresenter(this);
        return pagePresenter;
    }

    /**
     * 初始化页面
     */
    public void initView() {

        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("应用中心");
        token = SystemConfigFactory.getInstance(AppListActivity.this).getSystemConfig().getToken();

        adapter = new AppListAdapter(this, list);
        appListview.setAdapter(adapter);
    }

    /**
     * 点击
     */
    public void onClick() {
        appListview.setPullLoadEnabled(true);
        appListview.setOnRefreshListener(this);
        appListview.setOnItemClickListener(this);
    }

    /**
     * 加载数据
     */
    public void initData() {
        pagePresenter.getApplist(token);
        showLoading();
    }

    @OnClick(R.id.title_layout_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.e("sss5454", "onItemClick: "+list.get(position).getClickType());
        if(list.get(position).getClickType().equals("1")){
            Log.e("sss5454", "onItemClick: "+list.get(position).getWebUrl()+list.get(position).getAppName() );
            Intent intent = new Intent(this, BannerWebActivity.class);
            intent.putExtra("contentsrc", list.get(position).getWebUrl());
            intent.putExtra("name", list.get(position).getAppName());
            intent.putExtra("type", "hd");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }else{
            if (list.get(position).getAndroidSchemes() != null) {
            if (CommonUtils.isAvilible(AppListActivity.this, list.get(position).getAndroidSchemes())) {
                Intent intent = getPackageManager().getLaunchIntentForPackage(list.get(position).getAndroidSchemes());//这个是另外一个应用程序的包名
                startActivity(intent);
            } else {//未安装，跳转至market下载该程序
                if (list.get(position).getAndroidDown() != null) {

                    if (list.get(position).getAndroidDown().contains("http") || list.get(position).getAndroidDown().contains("https")) {
                        Uri uri = Uri.parse(list.get(position).getAndroidDown());
                        Intent it = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(it);
                    } else {
                        ToastUtil.show(AppListActivity.this, "暂无应用链接");
                    }
                } else {
                    ToastUtil.show(AppListActivity.this, "暂无应用链接");
                }
            }
        } else {
            ToastUtil.show(AppListActivity.this, "暂无应用链接");
        }
        }
    }

    @Override
    public void getAppList(AppListBean result) {

        dismissLoading();
        appListview.onPullDownRefreshComplete();
        appListview.onPullUpRefreshComplete();
        list = new ArrayList<>();
        list = result.getList();
        adapter.setLists(list);
    }

    @Override
    public void erroy() {
        dismissLoading();
        appListview.onPullDownRefreshComplete();
        appListview.onPullUpRefreshComplete();
        ToastUtil.show(AppListActivity.this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
            appListview.onPullDownRefreshComplete();
        appListview.onPullUpRefreshComplete();
        ToastUtil.show(AppListActivity.this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(AppListActivity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(AppListActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        appListview.onPullDownRefreshComplete();
        appListview.onPullUpRefreshComplete();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        appListview.onPullDownRefreshComplete();
        appListview.onPullUpRefreshComplete();
    }
}
